#include    "ftd.h"
#include    "mem.h"
#include    "utils.h"
#include    "logger.h"
#include    "def.h"

#include    <string.h>
#include    <cstdlib>
#include    <fstream>
#include    <iomanip>
#include    <iostream>  
#include    <string>

#include "v_match.h"
#include "utils.h"

VMatch g_vm;
MemCache<OrderStub, 100, 100> g_order_stub_mem;

int g_order_id_gen = 0;

void HelpInfo()
{
	printf("\n|------------------Help---------------------------------------------|");
	printf("\n|   1:Add Orders(10000)	, 2:				, 3:		          |");
	printf("\n|   4:					, 5:				, 6:		          |");
	printf("\n|   7:					, 8:Match by Market	, 9:Exit	          |");
	printf("\n|-------------------------------------------------------------------|");

}

void AddOrders()
{
	INT64 ns = lk_clock::milli_time();

	for(int i = 0; i < 5000; i++)
	{
		OrderStub* order = g_order_stub_mem.Alloc();
		order->BS = BUY;
		order->InstrumentId = 123;
		order->OrderId = g_order_id_gen++;
		order->Price = 3000;
		order->RemainVol = 1;
		order->Type = 0;

		g_vm.AddOrder(order);
	}

	for(int j = 0; j < 5000; j++)
	{
		OrderStub* order = g_order_stub_mem.Alloc();
		order->BS = SELL;
		order->InstrumentId = 123;
		order->OrderId = g_order_id_gen++;
		order->Price = 2999;
		order->RemainVol = 1;
		order->Type = 0;

		g_vm.AddOrder(order);
	}

	INT64 ne = lk_clock::milli_time();

#ifdef __WIN32__
	printf("AddOrders Time %I64d\r\n", ne-ns);
#else
	printf("AddOrders Time %lld\r\n", ne-ns);
#endif
}

void VirtualMatch()
{
	INT64 ns = lk_clock::milli_time();
	MarketMatchDataChgField pankou;
	pankou.LastPrice= 2999.6;
	pankou.BidPrice = 2999.8;
	pankou.AskPrice = 2999.4;

	g_vm.Match(&pankou);
	INT64 ne = lk_clock::milli_time();
#ifdef __WIN32__
	printf("VirtualMatch Time %I64d\r\n", ne-ns);
#else
	printf("VirtualMatch Time %lld\r\n", ne-ns);
#endif
}

int test_process(int argc, char **argv)
{
	char ch=0;
	HelpInfo();

	while(1){
			ch = getchar();
			if(ch == 49) AddOrders();                        //Num 1
			//if(ch == 50) HelpPos();                          //Num 2
			//if(ch == 51) HelpTrade();						 //Num 3
			//if(ch == 52) HelpCash();						 //Num 4
			//if(ch == 53) HelpPrice();						 //Num 5
			//if(ch == 54) HelpTicker();                       //Num 6
			//if(ch == 55) Login();		                     //Num 7
			if(ch == 56) VirtualMatch();					         //Num 8
			if(ch == 57) break;						 //Num 9

			//if(ch == 103) AddOrder();						  //g
			//if(ch == 102) DeleteOrder();					  //f
			//if(ch == 100) ChangeOrder();                      //d
			//if(ch == 115) LoadOrderReport();                  //s
			//if(ch == 97)  GetOrder();					      //a
			//if(ch == 120) SetOrderInactive();                 //x
			//if(ch == 113)  OrderCount();                      //q

			//if(ch == 104) PosCount();                          //h
			//if(ch == 106) GetPos();							   //j
			//if(ch == 107) GetPosByProduct();				   //k

			//if(ch == 116) TradeCount();						   //t
			//if(ch == 114) GetTrade();						   //r
			//if(ch == 101) GetTradeByProduct();				   //e
			//if(ch == 119) LoadTradeReport();				   //w

			//if(ch == 121) SubscribeUninitPrice(1);				//y
			//if(ch == 117) GetPriceCount();						//u
			//if(ch == 105) GetPrice();							//i
			//if(ch == 111) GetPriceByCode();						//o
			//if(ch == 112) SubscribeUninitPrice(0);              //p
			//if(ch == 91)  ;							            //[
			//if(ch == 93)  GetInstrument();						//]
			//if(ch == 92)  GetInstrumentByCode();				//'\'
			//if(ch == 59)  GetProduct();							//;
			//if(ch == 39)  GetProductByCode();					//'

			//if(ch == 110) GetAccInfo();							//n
			//if(ch == 98)  GetAccBalCount();                     //b
			//if(ch == 118) GetAccBal();							//v   
			//if(ch == 99)  GetAccBalByCurrency();				//c
			//if(ch == 122) GetConnectionStatus();				//z

			//if(ch == 44)  SubscribeTicker();                    //,
			//if(ch == 46)  UninitTicker();                       //.
			u_sleep(10);
	}

	return RETURN_SUCCESS;
}        // ----------  end of function main  ---------- 

